﻿using DnsClient;
using DnsClient.Protocol;
using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //DnsClientTest();
                DnsClient_GetDomainResult_WithExpectedUrlTestAsync().Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
            Console.WriteLine("Hello World!");
        }

        private static void DnsClientTest()
        {
            var uri = "127.0.0.1";
            IPAddress.TryParse(uri, out IPAddress iPAddress);
            var dnsQuery = new LookupClient(iPAddress, 8600);
            var services = dnsQuery.ResolveService("service.consul", "user_api");
            var addressList = services.First().AddressList;
            var host = addressList.Any() ? addressList.First().ToString() : services.First().HostName;
            var port = services.First().Port;
            var url = $"http://{host}:{port}";
            Console.WriteLine(url);
        }
        private static async Task DnsClient_GetDomainResult_WithExpectedUrlTestAsync()
        {
            var uri = "127.0.0.1";
            IPAddress.TryParse(uri, out IPAddress iPAddress);
            var dnsQuery = new LookupClient(iPAddress, 8600);
            var response = await dnsQuery.QueryAsync("baidu.com", QueryType.A);
            var record = response
   .Answers.OfRecordType(ResourceRecordType.HINFO)
   .FirstOrDefault() as HInfoRecord;
            Console.WriteLine($"OS:{record.OS},\tCPU:{record.Cpu}");
            
        }
    }
}
